import requests
from bs4 import BeautifulSoup

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'}
login_url= 'https://zh.flightaware.com/account/session'
login_data= {
        #'referer': 'https://zh.flightaware.com/account/session',
        'mode': 'login',
        'flightaware_username': 'huangkai39',
        'flightaware_password': '1qazxsw2'
    }

# 登录，获取指定航班160天的链接
def parse_flight(s, id):
    try:
        
        #r.text
        flight_rul= 'https://zh.flightaware.com/live/flight/'+id+'/history/160'
        f = s.get(flight_rul, headers=headers)
        print( f.status_code)
        #print(f.text)
        soup = BeautifulSoup(f.text, "lxml")
        records= soup.find_all("tr", class_="rowClickTarget")
        flights=[]
        for r in records:
            #print ( r['data-target'])
            flights.append( r['data-target'])

        return flights
    except  Exception as e:
        print('Error: '+ str(e))
    
import mysql.connector
# 保存到数据库
def save_flight(fleet_id, flights):
    try:
        cnx = mysql.connector.connect(user='flightdata', password='flightdata',
                                  host='127.0.0.1',
                                  database='flightdata')
        cursor = cnx.cursor()
        cursor.execute("create table if not exists flight(id int auto_increment primary key, fleet_id varchar(10), \
                       url varchar(100) not null unique );")
        add_fleet = "INSERT ignore INTO flight  \
                      ( fleet_id, url) \
                      VALUES (%s,%s)"
        for url in flights:
            #print(f['id'])
            cursor.execute(add_fleet, (fleet_id, url))

        cnx.commit()

        cursor.close()
        cnx.close()
    except  Exception as e:
        print('Error: '+ str(e))

    
if __name__ == "__main__":
    # 获取所有航班
    cnx = mysql.connector.connect(user='flightdata', password='flightdata',
                                  host='127.0.0.1',
                                  database='flightdata')
    cursor = cnx.cursor()
    cursor.execute("select id from fleet ")
    with requests.Session() as s:
        r= s.post(login_url, data=login_data, headers=headers)
        print(r.status_code)

        for id in cursor:
            print (id[0])
            fleet_id= id[0]
            flights= parse_flight(s, fleet_id)
            print("Get flights:"+str(len(flights)))

            save_flight(fleet_id, flights)